function getExplorerType(){
	var explorer = window.navigator.userAgent;
	if(explorer.indexOf("MSIE") >= 0){// IE
		return "IE";
	}else if(explorer.indexOf("Firefox") >= 0){// Firefox
		return "Firefox";
	}else if(explorer.indexOf("Chrome") >= 0){// Chrome
		return "Chrome";
	}else if(explorer.indexOf("Opera") >= 0){// Opera
		return "Opera";
	}else if(explorer.indexOf("Safari") >= 0){// Safari
		return "Safari";
	}else{
		return "Unknowned";
	}
}

function alertExplorerType(){
	if(getExplorerType() == "IE"){
		// 利用对话框返回的值 （true 或者 false）
		if(confirm("您正在使用IE浏览器，为了更好的显示效果，请使用Firefox或者Chrome浏览器.")){
			window.open("http://www.firefox.com.cn/download/");
		}
	}
}

$(function(){
	// 屏蔽右键
	document.oncontextmenu = function(e){
		return false;
	}

	document.onkeydown = function(e){
		if(event.keyCode == 116){
			event.keyCode = 0;
			event.cancelBubble = true;
			return false;
		}
	}
});

var getXMLHttpRequest = function(){
	var xhr = null;
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xhr;
}

var httpRequestGet = function(url, async, onloadFunc){
	var xhr = getXMLHttpRequest();
	if(async == true){
		xhr.open('GET', url, true);
	}else{
		xhr.open('GET', url, false);
	}
	xhr.setRequestHeader('Content-Type', 'application/json');
	xhr.setRequestHeader('Accept', 'application/json');
	xhr.onreadystatechange = function(){
		if(xhr.status == 0){
			// mui.toast("网络不给力！！！");
			return;
		}else if(xhr.status == 200){
			onloadFunc(xhr);
		}else if(xhr.status == 404){
			// mui.toast("服务器错误，错误代码:" + xhr.status);
			return;
		}
	};
	xhr.onerror = function(){
		if(xhr.status == 0){
			// mui.toast("网络不给力！！！");
			return;
		}
	}
	xhr.send();
}

var httpRequestGetJson = function(url, async, onloadFunc){
	var xhr = getXMLHttpRequest();
	if(async == true){
		xhr.open('GET', url, true);
	}else{
		xhr.open('GET', url, false);
	}
	xhr.setRequestHeader('Content-Type', 'application/json');
	xhr.setRequestHeader('Accept', 'application/json');
	xhr.onreadystatechange = function(){
		if(xhr.status == 0){
			// mui.toast("网络不给力！！！");
			return;
		}else if(xhr.status == 200){
			onloadFunc(JSON.parse(xhr.responseText));
		}else if(xhr.status == 404){
			// mui.toast("服务器错误，错误代码:" + xhr.status);
			return;
		}
	};
	xhr.onerror = function(){
		if(xhr.status == 0){
			// mui.toast("网络不给力！！！");
			return;
		}
	}
	xhr.send();
}

var httpRequestPost = function(url, sendJson, async, onloadFunc){
	var xhr = getXMLHttpRequest();
	if(async == true){
		xhr.open('POST', url, true);
	}else{
		xhr.open('POST', url, false);
	}
	xhr.setRequestHeader('Content-Type', 'application/json');
	xhr.setRequestHeader('Accept', 'application/json');
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			onloadFunc(xhr);
		}else{
			return;
		}
	};
	xhr.send(sendJson);
}

var httpRequestPostJson = function(url, sendJson, async, onloadFunc){
	$.ajax({
	    url : url,
	    type : 'POST',
	    async : async,
	    cache : false,
	    data : JSON.stringify(sendJson),
	    dataType : 'json',
	    contentType : 'application/json',
	    error : function(result){
		    bootbox.setLocale("zh_CN");
		    bootbox.alert("提交出错，请联系管理员！", function(result){
		    });
	    },
	    success : function(result){
		    onloadFunc(result);
	    }
	});
}

var httpRequestPostJsonNoReturn = function(url, sendJson, async, onloadFunc){
	$.ajax({
	    url : url,
	    type : 'POST',
	    async : async,
	    cache : false,
	    data : JSON.stringify(sendJson),
	    dataType : 'json',
	    contentType : 'application/json',
	    error : function(result){
	    },
	    success : function(result){
	    }
	});
}

var httpRequestPostSubmitJson = function(form, url, sendJson, async, onloadFunc){
	var btnOldLabel = $(form).find(":submit").html();
	if(btnOldLabel == undefined){
		btnOldLabel = $(form).find(":submit").val();
		if(btnOldLabel == undefined){
			btnOldLabel = "提交";
		}
	}
	$(form).find(":submit").attr("disabled", true).html("提交中...").val("提交中...");
	$.ajax({
	    url : url,
	    type : 'POST',
	    async : async,
	    cache : false,
	    data : JSON.stringify(sendJson),
	    dataType : 'json',
	    contentType : 'application/json',
	    error : function(result){
		    $(form).find(":submit").attr("disabled", false).html(btnOldLabel).val(btnOldLabel);
		    bootbox.setLocale("zh_CN");
		    bootbox.alert("提交出错，请联系管理员！", function(result){
		    });
	    },
	    success : function(result){
		    $(form).find(":submit").attr("disabled", false).html(btnOldLabel).val(btnOldLabel);
		    onloadFunc(result);
	    }
	});
}

var httpRequestPostSubmitForm = function(form, url, async, onloadFunc){
	var btnOldLabel = $(form).find(":submit").html();
	if(btnOldLabel == undefined){
		btnOldLabel = $(form).find(":submit").val();
		if(btnOldLabel == undefined){
			btnOldLabel = "提交";
		}
	}
	$(form).find(":submit").attr("disabled", true).html("提交中...").val("提交中...");
	var formData = new FormData(form);
	$.ajax({
	    url : url,
	    type : 'POST',
	    data : formData,
	    async : async,
	    cache : false,
	    contentType : false,
	    processData : false,
	    success : function(result){
		    $(form).find(":submit").attr("disabled", false).html(btnOldLabel).val(btnOldLabel);
		    onloadFunc(result);
	    },
	    error : function(result){
		    $(form).find(":submit").attr("disabled", false).html(btnOldLabel).val(btnOldLabel);
		    onloadFunc(result);
	    }
	});
}

var del = function(id){
	if(window.confirm("你确定删除吗？")){
		var url = "delete/" + id + ".do";
		var entity = {};
		var onloadFunc = function(result){
			if(result.isSuccess == true){
				history.go(0);
			}else{
				alert("删除失败！");
			}
		};
		
		httpRequestPostJson(url, entity, false, onloadFunc);
	}
}

function toJson(obj){
	return JSON.stringify(obj);
}
function fromJson(val){
	return JSON.parse(val);
}

function getSelectedArray(name){
	var items = document.getElementsByName(name);
	var result = [];
	var count = 0;
	for(var i = 0;i < items.length;i++){
		if(items[i].checked){
			result[count] = items[i].value;
			count++;
		}
	}
	return result;
}
function checkAll(name){
	var items = document.getElementsByName(name);
	var checked = !items[0].checked;
	for(var i = 0;i < items.length;i += 1){
		items[i].checked = checked;
	}
}
// 选中
function ListCheckAll(obj){
	if(obj.checked){
		$('input[name=id]').prop('checked', 'true');
	}else{
		$('input[name=id]').removeAttr('checked');
	}
}
function ListCheck(obj){
	var list = $('input[name=id]');
	var val = (function(){
		for(var i = 0;i < list.length;i++){
			if(list[i].checked){
				
			}else{
				return;
			}
		}
		return true;
	})();
	if(obj.checked && val == true){
		$('#selectAll').prop('checked', 'true');
	}else{
		$('#selectAll').removeAttr('checked');
	}
}
